package com.zhiyou100.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.model.User;
import com.zhiyou100.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("update.do")
	public String update(User user){
		userService.update(user);
		return "redirect:show.do";
	}
	
	@RequestMapping("edit.do")
	public String edit(Model model,int id){
		model.addAttribute("user",userService.queryById(id));
		return "user/edit";
	}
	
	@RequestMapping("delete.do")
	public String delete(int id){
		userService.deleteById(id);
		return "redirect:show.do";
	}
	
	@RequestMapping("insert.do")
	public String insert(User user){
		userService.add(user);
		return "redirect:show.do";
	}
	
	@RequestMapping("add.do")
	public String add(){
		return "user/add";
	}
	
	@RequestMapping("show.do")
	public String show(Model model){
		System.out.println(userService.queryAll()+"+++++++++++++++");
		model.addAttribute("users", userService.queryAll());
		return "user/show";
	}
}
